from typing import Optional


class User:
    """用户实体类"""
    def __init__(self,
                 id: Optional[int] = None,
                 username: Optional[str] = None,
                 id_card: Optional[str] = None,
                 last_login: Optional[str] = None):
        """
        初始化用户实体
        :param id: 用户ID
        :param username: 用户名
        :param id_card: 身份证号
        :param last_login: 最后登录时间
        """
        self.id = id
        self.username = username
        self.id_card = id_card
        self.last_login = last_login

    def __dict__(self) -> dict:
        """将实体转换为字典"""
        return {
            'id': self.id,
            'username': self.username,
            'id_card': self.id_card,
            'last_login': self.last_login
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'User':
        """从字典创建实体"""
        return cls(
            id=data.get('id'),
            username=data.get('username'),
            id_card=data.get('id_card'),
            last_login=data.get('last_login')
        )